#region Using Directives

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using C4F.DevKit.PowerManagement;

#endregion

namespace PowerManagementApplication
{
    public partial class frmPowerManagement : Form
    {
        #region Global Data

        PowerManager powerManager = new PowerManager();
        PowerScheme schemeValue = null;
        ToolTip toolTip = new ToolTip();

        #endregion

        #region Methods

        #region Tab Initialize methods
        
        /// <summary>
        /// Constructor. Keeps GUI in particular state at start.
        /// </summary>
        public frmPowerManagement()
        {
            InitializeComponent();
            cmbModes.SelectedIndex = 0;
            InitializeWindowsSecurityTab();
            cbxSetTimer.Enabled = false;
            cbxOptionForce.Enabled = false;
        }

        /// <summary>
        /// Keeps controls of Tab-page 'Windows Security' in initial state.
        /// </summary>
        private void InitializeWindowsSecurityTab()
        {
            cbxOptionForce.Enabled = true;
            cbxOptionForce.Checked = false;
            cbxDisableWakeEvent.Enabled = false;
            cbxDisableWakeEvent.Checked = false;
            cbxSetTimer.Enabled = true;
            cbxSetTimer.Checked = false;
            gbxTimerSettings.Enabled = false;
        }

        /// <summary>
        /// Keeps controls of Tab-page 'Power Schemes'in initial state.
        /// </summary>
        private void InitializePowerSchemeTab()
        {
            btnRefresh.Enabled = true;
            btnApply.Enabled = false;
            btnDelete.Enabled = false;
        }

        /// <summary>
        /// Keeps controls of Tab-page 'Battery Status'in initial state.
        /// </summary>
        private void InitializeBatteryStatusTab()
        {
            tbxBatteryChargeStatus.Text = string.Empty;
            tbxBatteryLife.Text = string.Empty;
            tbxIsCharging.Text = string.Empty;
        }

        /// <summary>
        /// Keeps label controls of Tab-page 'Battery Status' according to state parameter
        /// passed.
        /// </summary>
        /// <param name="state"></param>
        private void InitializeBatteryStatusLabels(bool state)
        {
            lblBatteryStatus.Enabled = state;
            lblIsCharging.Enabled = state;
            lblBatteryLife.Enabled = state;
            lblBatteryChargeState.Enabled = state;
            progressBarBatteryLife.Enabled = state;
            lblBatteryLifePercentage.Enabled = state;
        }

        #endregion

        #region Windows Secuirity Methods

        /// <summary>
        /// Shutdowns machine based on force and timer parameters. 
        /// </summary>
        private void Shutdown()
        {
            try
            {
                bool force = cbxOptionForce.Checked;
                string shutdownMessage = tbxMessage.Text;
                uint secondsTimer;
                if (cbxSetTimer.Checked)
                {
                    try
                    {
                        secondsTimer = Convert.ToUInt32(tbxTimeSeconds.Text);
                    }
                    catch
                    {
                        MessageBox.Show(
                            "Please specify positive integer value.",
                            "Power Management", MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
                        return;
                    }
                    powerManager.ShutdownComputer(
                        shutdownMessage, secondsTimer, force);
                }
                else
                {
                    powerManager.ShutdownComputer(force);
                }
            }
            catch (PowerManagerException ex)
            {
                MessageBox.Show(ex.Message,
                            "Power Management", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);

            }
            catch (ArgumentException ex)
            {
                MessageBox.Show(ex.Message,
                            "Power Management", MessageBoxButtons.OK,
                            MessageBoxIcon.Exclamation);
            }
        }

        /// <summary>
        /// Reboots machine based on force and timer parameters. 
        /// </summary>
        private void Reboot()
        {
            try
            {
                bool force = cbxOptionForce.Checked;
                string shutdownMessage = tbxMessage.Text;
                uint secondsTimer;
                if (cbxSetTimer.Checked)
                {
                    try
                    {
                        secondsTimer = Convert.ToUInt32(tbxTimeSeconds.Text);
                    }
                    catch
                    {
                        MessageBox.Show(
                            "Please specify positive integer value.",
                            "Power Management", MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
                        return;
                    }
                    powerManager.RebootComputer(
                        shutdownMessage, secondsTimer, force);
                }
                else
                {
                    powerManager.RebootComputer(force);
                }
            }
            catch (PowerManagerException ex)
            {
                MessageBox.Show(ex.Message, 
                            "Power Management", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
            catch (ArgumentException ex)
            {
                MessageBox.Show(ex.Message, 
                            "Power Management", MessageBoxButtons.OK,
                            MessageBoxIcon.Exclamation);
            }
        }

        /// <summary>
        /// Logs off current with\without force.
        /// </summary>
        private void LogOff()
        {
            try
            {
                bool force = cbxOptionForce.Checked;
                powerManager.LogOffCurrentUser(force);
            }
            catch (PowerManagerException ex)
            {
                MessageBox.Show(ex.Message, 
                            "Power Management", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Poweroff the computer
        /// </summary>
        private void PowerOff()
        {
            try
            {
                bool force = cbxOptionForce.Checked;
                powerManager.PowerOffComputer(force);
            }
            catch (PowerManagerException ex)
            {
                MessageBox.Show(ex.Message, 
                            "Power Management", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Puts computer in hibernate state.
        /// </summary>
        private void Hibernate()
        {
            try
            {
                bool force = cbxOptionForce.Checked;
                powerManager.HibernateComputer(force);
            }
            catch (PowerManagerException ex)
            {
                MessageBox.Show(ex.Message, 
                            "Power Management", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
            catch (InvalidOperationException ex)
            {
                MessageBox.Show(ex.Message, 
                            "Power Management", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Puts computer in Standby state.
        /// </summary>
        private void StandByComputer()
        {
            try
            {
                bool force = cbxOptionForce.Checked;
                bool disableWakeEvent = cbxDisableWakeEvent.Checked;
                powerManager.StandbyComputer(force, disableWakeEvent);
            }
            catch (PowerManagerException ex)
            {
                MessageBox.Show(ex.Message, 
                            "Power Management", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Puts workstation in locked state.
        /// </summary>
        private void LockWorkStation()
        {
            try
            {
                powerManager.LockWorkStation();
            }
            catch (PowerManagerException ex)
            {
                MessageBox.Show(ex.Message, 
                            "Power Management", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
        }

        #endregion

        #region Power schemes Methods

        /// <summary>
        /// Shows active power scheme in 'Active Power Scheme' textbox.
        /// </summary>
        private void ShowActivePowerScheme()
        {
            try
            {
                tbxActivePowerScheme.Text = 
                    powerManager.GetActivePowerScheme();
            }
            catch (PowerManagerException ex)
            {
                MessageBox.Show(ex.Message, 
                            "Power Management", MessageBoxButtons.OK, 
                            MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Shows all available power schemes in listbox provided.
        /// </summary>
        private void ShowAvailablePowerScheme()
        {
            try
            {
                lbxAvailablePowerSchemes.Items.Clear();
                List<string> powerSchemes = 
                    powerManager.GetAvailablePowerSchemes();
                foreach (string scheme in powerSchemes)
                    lbxAvailablePowerSchemes.Items.Add(scheme);
            }
            catch (PowerManagerException ex)
            {
                MessageBox.Show(ex.Message, 
                            "Power Management", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
        }

        #endregion

        #region BatteryStatus Methods

        /// <summary>
        /// This function contains code to show battery details on 
        /// Tab-page 'Battery Status'
        /// </summary>
        /// <param name="batteryStatus"></param>
        private void RenderBatteryValues(BatteryStatus batteryStatus)
        {
            BatteryFlag flag = batteryStatus.BatteryFlag & ~BatteryFlag.Charging;
            tbxBatteryChargeStatus.Text = flag.ToString();
            tbxBatteryLife.Text = batteryStatus.BatteryLifePercent.ToString()+ "%";
            tbxIsCharging.Text = batteryStatus.Charging.ToString();
            if (batteryStatus.BatteryPresent)
            {
                progressBarBatteryLife.Value = batteryStatus.BatteryLifePercent;
                lblBatteryLifePercentage.Text = batteryStatus.BatteryLifePercent.ToString() + "%";
            }
        }

        /// <summary>
        /// Function checks whether battery present in machine or not.
        /// If yes renders battery attributes in textboxes provided on page.
        /// </summary>
        private void CheckForBattery()
        {
            try
            {
                BatteryStatus batteryStatus = powerManager.GetBatteryStatus();
                if (batteryStatus.BatteryPresent)
                {
                    lblBatteryMessage.Text = string.Empty;
                    RenderBatteryValues(batteryStatus);
                    InitializeBatteryStatusLabels(true);
                }
                else
                {
                    lblBatteryMessage.ForeColor = Color.Red;
                    lblBatteryMessage.Text = "Battery not found.";
                    InitializeBatteryStatusTab();
                    InitializeBatteryStatusLabels(false);
                }
            }
            catch (PowerManagerException ex)
            {
                MessageBox.Show(ex.Message, "Power Management", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
        }

        #endregion

        #endregion

        #region Events

        /// <summary>
        /// Initializes the tooltip object.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmPowerManagement_Load(object sender, EventArgs e)
        {
            toolTip.AutoPopDelay = 5000;
            toolTip.InitialDelay = 1;
            toolTip.ReshowDelay = 1;
            toolTip.ShowAlways = true;
        }

        #region Tab Control Events

        /// <summary>
        /// This function contains code to keeping controls in particular state 
        /// while navigating through tabs.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tabctrlPowerManagement_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectedIndex = tabctrlPowerManagement.SelectedIndex;

            switch (selectedIndex)
            {
                case 0:
                    DisableTimer();
                    break;
                case 1:
                    InitializePowerSchemeTab();
                    ShowActivePowerScheme();
                    ShowAvailablePowerScheme();
                    DisableTimer();
                    break;
                case 2:
                    InitializeBatteryStatusTab();
                    CheckForBattery();
                    EnableTimer();
                    break;
                case 3:
                    InitializePowerSettingTab();
                    DisableTimer();
                    break;

            }
        }

        /// <summary>
        /// This function contains code to exit application.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btmClose_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        #endregion

        #region Windows Secuirity Events

        /// <summary>
        /// This function changes the other controls state of 'Windows Security' 
        /// Tab-page as per operation selected in combobox.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbModes_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectedIndex = cmbModes.SelectedIndex;
            switch(selectedIndex)
            {
                //shutdown
                case 1 :
                    InitializeWindowsSecurityTab();
                    break;
                //Restart
                case 2 :
                    InitializeWindowsSecurityTab();
                    break;
                //Log Off
                case 3 :
                    InitializeWindowsSecurityTab();
                    cbxSetTimer.Enabled = false;
                    break;
                //Hibernate
                case 4:
                    InitializeWindowsSecurityTab();
                    cbxSetTimer.Enabled = false;
                    break;
                //PowerOff
                case 5:
                    InitializeWindowsSecurityTab();
                    cbxSetTimer.Enabled = false;
                    break;
                //Stand By
                case 6:
                    InitializeWindowsSecurityTab();
                    cbxSetTimer.Enabled = false;
                    cbxDisableWakeEvent.Enabled = true;
                    break;
                //Lock System
                case 0:
                    InitializeWindowsSecurityTab();
                    cbxSetTimer.Enabled = false;
                    cbxOptionForce.Enabled = false;
                    break;            
            }
        }

        /// <summary>
        /// This function decides 'TimeSettings' groupbox's enable state on 
        /// 'SetTimer' checkbox's check state.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbxSetTimer_CheckStateChanged(object sender, EventArgs e)
        {
            if (cbxSetTimer.CheckState == CheckState.Checked)
                gbxTimerSettings.Enabled = true;
            else
                gbxTimerSettings.Enabled = false;
        }

        /// <summary>
        /// This function clears 'TimeSeconds' and 'Message' textboxes if 'TimeSettings'
        /// groupbox turns to disable state.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gbxTimerSettings_EnabledChanged(object sender, EventArgs e)
        {
            if (gbxTimerSettings.Enabled == false)
            {
                tbxTimeSeconds.Text = string.Empty;
                tbxMessage.Text = string.Empty;
            }
        }

        /// <summary>
        /// This function covers functionality to perform action as per operation 
        /// selected in comboBox.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            int selectedIndex = cmbModes.SelectedIndex;

            // No confirmation for locking workstation
            if (selectedIndex > 0)
                if (MessageBox.Show("Are you sure?", "Power Management", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
                    return;
            switch (selectedIndex)
            {
                //shutdown
                case 1:
                    Shutdown();
                    break;
                //Restart
                case 2:
                    Reboot();
                    break;
                //Log Off
                case 3:
                    LogOff();
                    break;
                //Hibernate
                case 4:
                    Hibernate();
                    break;
                //PowerOff
                case 5:
                    PowerOff();
                    break;
                //Stand By
                case 6:
                    StandByComputer();
                    break;
                //Lock System
                case 0:
                    LockWorkStation();
                    break;
            }
        }

        #endregion

        #region Power Schemes Events

        /// <summary>
        /// Functionality to enable 'Apply' and 'Delete' buttons 
        /// when item from listbox is selected.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lbxAvailablePowerSchemes_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnApply.Enabled = true;
            btnDelete.Enabled = true;
        }

        /// <summary>
        /// Functionality to refresh power scheme related data on Tab-page.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            ShowActivePowerScheme();
            ShowAvailablePowerScheme();
        }

        /// <summary>
        /// Functionality to apply the selected powerscheme as active power scheme.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnApply_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Are you sure?", 
                            "Power Management", MessageBoxButtons.OKCancel, 
                            MessageBoxIcon.Question) == DialogResult.OK)
                    powerManager.SetActivePowerScheme(
                            lbxAvailablePowerSchemes.Text);
                else
                    return;
            }

            catch (PowerManagerException ex)
            {
                MessageBox.Show(ex.Message, 
                            "Power Management", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }

            catch (ArgumentNullException ex)
            {
                MessageBox.Show("Please Select Power scheme", 
                            "Power Management", MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
            }

            catch (ArgumentException ex)
            {
                MessageBox.Show(ex.Message, 
                            "Power Management", MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
            }

            ShowActivePowerScheme();
        }

        /// <summary>
        /// Functionality to delete any power scheme.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Are you sure?", 
                            "Power Management", MessageBoxButtons.OKCancel, 
                            MessageBoxIcon.Question) == DialogResult.OK)
                    powerManager.DeletePowerScheme(lbxAvailablePowerSchemes.Text);
                else
                    return;
            }

            catch (PowerManagerException ex)
            {
                MessageBox.Show(ex.Message, 
                            "Power Management", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }

            catch (ArgumentNullException ex)
            {
                MessageBox.Show("Please Select Power scheme", 
                            "Power Management", MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
            }

            catch (ArgumentException ex)
            {
                MessageBox.Show(ex.Message, 
                            "Power Management", MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
            }

            ShowAvailablePowerScheme();
        }

        #endregion

        /// <summary>
        /// When called it initializes the battery status tab.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            InitializeBatteryStatusTab();
            CheckForBattery();
        }

        /// <summary>
        /// Enables the timer.
        /// </summary>
        private void EnableTimer()
        {
            tmrRenderValues.Enabled = true;
            tmrRenderValues.Start(); 
        }

        /// <summary>
        /// Disables the timer.
        /// </summary>
        private void DisableTimer()
        {
            tmrRenderValues.Stop();
            tmrRenderValues.Enabled = false;          
        }

        #endregion

        private void DisableComboBox(ComboBox cmbBox)
        {
            cmbBox.Items.Clear();
            cmbBox.Enabled = false;
        }


        /// <summary>
        /// Keeps controls of Tab-page 'Power Setting'in initial state.
        /// </summary>
        private void InitializePowerSettingTab()
        {
            // Populate the combo box
            try
            {
                btnGetSettings.Enabled = false;
                
                // Get the active scheme
                string activeScheme =
                   powerManager.GetActivePowerScheme();

                cmbSchemes.Items.Clear();
                ClearPowerSettingTextBoxes();

                DisableComboBox(cmbSubGroup);
                
                DisableComboBox(cmbSetting);
               
                List<string> powerSchemes =
                    powerManager.GetAvailablePowerSchemes();

                foreach (string scheme in powerSchemes)
                {
                    cmbSchemes.Items.Add(scheme);
                }
            }
            catch (PowerManagerException ex)
            {
                MessageBox.Show(ex.Message,
                            "Power Management", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Initializes the power setting list and clear the text boxes.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbSubGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitializeSettingsList();
            ClearPowerSettingTextBoxes();
        }

        /// <summary>
        /// Initializes the sub group list, clear the power setting list and the text boxes.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbSchemes_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitializeSubGroupList();
            cmbSetting.Items.Clear();          
            ClearPowerSettingTextBoxes();
        }

        /// <summary>
        /// Initializes the sub group list.
        /// </summary>
        private void InitializeSubGroupList()
        {
            
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                cmbSubGroup.Enabled = true;
                string activeScheme = powerManager.GetActivePowerScheme();
                if (rdoAcSettings.Checked)
                {
                    schemeValue = powerManager.ReadPowerSchemeACValue(activeScheme);
                }
                else
                {
                    schemeValue = powerManager.ReadPowerSchemeDCValue(activeScheme);
                }

                cmbSubGroup.Items.Clear();
                foreach (SettingSubGroup subgroup in schemeValue.SubGroups)
                {
                    cmbSubGroup.Items.Add(subgroup.Name);
                }
                Cursor.Current = Cursors.Default;
            }
            catch (PowerManagerException ex)
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show(ex.Message,
                            "Power Management", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }

            catch (ArgumentNullException ex)
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show(ex.Message,
                            "Power Management", MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
            }

            catch (ArgumentException ex)
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show(ex.Message,
                            "Power Management", MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
            }
        }

        /// <summary>
        /// Initializes the power setting list.
        /// </summary>
        private void InitializeSettingsList()
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                cmbSetting.Enabled = true;
                btnGetSettings.Enabled = false;
                if (rdoAcSettings.Checked)
                {
                    schemeValue = powerManager.ReadPowerSchemeACValue(cmbSchemes.Text);
                }
                else
                {
                    schemeValue = powerManager.ReadPowerSchemeDCValue(cmbSchemes.Text);
                }
                foreach (SettingSubGroup subgroup in schemeValue.SubGroups)
                {
                    if (String.Equals(subgroup.Name, cmbSubGroup.Text))
                    {
                        cmbSetting.Items.Clear();
                        foreach (PowerSetting setting in subgroup.PowerSettings)
                        {
                            cmbSetting.Items.Add(setting.Name);
                        }
                        break;
                    }
                }
                Cursor.Current = Cursors.Default;
            }
            catch (PowerManagerException ex)
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show(ex.Message,
                            "Power Management", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }

            catch (ArgumentNullException ex)
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show(ex.Message,
                            "Power Management", MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
            }

            catch (ArgumentException ex)
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show(ex.Message,
                            "Power Management", MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
            }
        }

        /// <summary>
        /// Clear the text boxes and the tooltips.
        /// </summary>
        private void ClearPowerSettingTextBoxes()
        {
            tbxSelectedScheme.Text = String.Empty;
            tbxSelectedSubGroup.Text = String.Empty;
            tbxSelectedSetting.Text = String.Empty;
            tbxSettingValue.Text = String.Empty;
            btnGetSettings.Enabled = false;
            toolTip.RemoveAll();
        }

        /// <summary>
        /// Gets the setting values and initializes the tooltip.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGetSettings_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            try
            {
                if (rdoAcSettings.Checked)
                {
                    schemeValue = powerManager.ReadPowerSchemeACValue(cmbSchemes.Text);
                }
                else
                {
                    schemeValue = powerManager.ReadPowerSchemeDCValue(cmbSchemes.Text);
                }
                foreach (SettingSubGroup subgroup in schemeValue.SubGroups)
                {
                    if (String.Equals(subgroup.Name, cmbSubGroup.Text))
                    {
                        foreach (PowerSetting setting in subgroup.PowerSettings)
                        {
                            if (String.Equals(setting.Name, cmbSetting.Text))
                            {
                                tbxSelectedScheme.Text = cmbSchemes.Text;
                                tbxSelectedSubGroup.Text = cmbSubGroup.Text;
                                tbxSelectedSetting.Text = cmbSetting.Text;
                                tbxSettingValue.Text = setting.Value;
                                toolTip.SetToolTip(this.tbxSelectedScheme, schemeValue.Description);
                                toolTip.SetToolTip(this.tbxSelectedSubGroup, subgroup.Description);
                                toolTip.SetToolTip(this.tbxSelectedSetting, setting.Description);
                                break;
                            }
                        }
                        break;
                    }
                }
                Cursor.Current = Cursors.Default;
            }
            catch (PowerManagerException ex)
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show(ex.Message,
                            "Power Management", MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
            }

            catch (ArgumentNullException ex)
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show(ex.Message,
                            "Power Management", MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
            }

            catch (ArgumentException ex)
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show(ex.Message,
                            "Power Management", MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
            }
        }

        /// <summary>
        /// Clear the text boxes and activates the button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbSetting_SelectedIndexChanged(object sender, EventArgs e)
        {
            ClearPowerSettingTextBoxes();
            btnGetSettings.Enabled = true;
        }

        /// <summary>
        /// Clear the text boxes.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rdoAcSettings_CheckedChanged(object sender, EventArgs e)
        {
            bool buttonEnabled = false;
            if (btnGetSettings.Enabled == true)
                buttonEnabled = true;
            ClearPowerSettingTextBoxes();
            if (buttonEnabled == true)
                btnGetSettings.Enabled = true;
        }

    }
}